//
//  YZLCarImageViewController.m
//  Car
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLCarImageViewController.h"
#import <UIImageView+WebCache.h>

@interface YZLCarImageViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation YZLCarImageViewController

#pragma mark - 创建scrollView
- (void)createScrollView {
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height - 300) / 2.f, self.view.frame.size.width, 300)];
    self.scrollView.autoresizesSubviews = NO;
//    self.scrollView.backgroundColor = [UIColor colorWithWhite:0.800 alpha:1.000];
    [self.view addSubview:self.scrollView];
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.imageArr.count, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    for (int i = 0; i < self.imageArr.count; i++) {
        UIImageView *largeImageView = [[UIImageView alloc] init];
        largeImageView.frame = CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, 300);

        [largeImageView sd_setImageWithURL:[[self.imageArr objectAtIndex:i] objectForKey:@"imageBig"] placeholderImage:[UIImage imageNamed:@"D0"]];

        [self.scrollView addSubview:largeImageView];
    }
    
    //价格
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, (self.view.frame.size.height - 300) / 2.f + 320, self.view.frame.size.width / 2.f + 60, 20)];
    label1.text = [NSString stringWithFormat:@"厂商指导价: %@", self.price];
    [self.view addSubview:label1];
    //降价
    if (![self.dropPrice isEqualToString:@"0"]) {
        
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"下降"]];
        image.frame = CGRectMake(self.view.frame.size.width / 2.f + 60, (self.view.frame.size.height - 300) / 2.f + 320, 20, 20);
        [self.view addSubview:image];
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2.f + 80, (self.view.frame.size.height - 300) / 2.f + 320, self.view.frame.size.width / 2 - 80, 20)];
        label2.text = [NSString stringWithFormat:@"直降%@", self.dropPrice];
        label2.textColor = [UIColor orangeColor];
        [self.view addSubview:label2];
    }
    
    
    self.scrollView.delegate = self;
    self.scrollView.contentOffset = CGPointMake(self.itemNum * self.view.frame.size.width, 0);
    
}

//代理方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    self.navigationItem.title = [NSString stringWithFormat:@"%0.f/%ld", scrollView.contentOffset.x / self.view.frame.size.width + 1, self.imageArr.count];    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = [NSString stringWithFormat:@"%ld/%ld", self.itemNum + 1, self.imageArr.count];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveImage)];
    
    [self createScrollView];
    
    //返回
    [self skipReturnWithTarget:self];
    
    //bar颜色
    [self navigationbarColor];
}

#pragma mark - 将图片保存到本地相册
- (void)saveImage {
    
    NSLog(@"保存");
    UIImageView *image = [[UIImageView alloc] init];
    NSString *str = [[self.imageArr objectAtIndex:(self.scrollView.contentOffset.x / self.view.frame.size.width)] objectForKey:@"imageBig"];
    NSURL *url = [NSURL URLWithString:str];
    [image sd_setImageWithURL:url];
    UIImageWriteToSavedPhotosAlbum(image.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    
    NSString *msg = nil ;
    if(error != NULL){
        msg = @"保存图片失败" ;
    }else{
        msg = @"保存图片成功!请到相册里查看" ;
    }
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    
    /* 根据照片来源确定AlertAction */
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"图片保存成功是否立即查看" preferredStyle:UIAlertControllerStyleActionSheet];
    
    /* 判断相册是否可以访问 */
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"立即前往" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentViewController:picker animated:NO completion:^{}];
            
            
        }];
        
        [alert addAction:action1];
    }
    
    UIAlertAction *cancel  =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    
    [alert addAction:cancel];

    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
